package cn.wenhe9.novel.manager;

import cn.wenhe9.novel.constant.CacheConsts;
import cn.wenhe9.novel.constant.DatabaseConsts;
import cn.wenhe9.novel.domain.dto.UserBookShelfDto;
import cn.wenhe9.novel.domain.dto.UserReadHistoryDto;
import cn.wenhe9.novel.domain.entity.user.UserBookshelf;
import cn.wenhe9.novel.domain.entity.user.UserReadHistory;
import cn.wenhe9.novel.mapper.UserBookshelfMapper;
import cn.wenhe9.novel.mapper.UserReadHistoryMapper;
import cn.wenhe9.novel.result.PageResultDto;
import cn.wenhe9.novel.result.ResultResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author DuJinliang
 * 2022/5/27
 */
@Component
public class UserReadHistoryCacheManager {
    @Resource
    private UserReadHistoryMapper userReadHistoryMapper;

    /**
     * 查询用户信息，并放入缓存中
     */
    @Cacheable(
            value = CacheConsts.USER_READ_HISTORY_CACHE_NAME,
            cacheManager = CacheConsts.REDIS_CACHE_MANAGER
//            keyGenerator = "keyGenerator"
    )
    public PageResultDto<UserReadHistoryDto> listReadHistory(Long userId, Long page, Long limit) {
        Page<UserReadHistoryDto> readHistoryDtoPage = userReadHistoryMapper.queryReadHistoryByPage(new Page<>(page, limit), userId);
        return PageResultDto.of(page, limit, readHistoryDtoPage.getTotal(), readHistoryDtoPage.getRecords());
    }


    @CacheEvict(
            value = CacheConsts.USER_READ_HISTORY_CACHE_NAME,
            cacheManager = CacheConsts.REDIS_CACHE_MANAGER,
            allEntries = true
    )
    public void addToReadHistory(Long userId, Long bookId, Long preContentId) {
        LambdaQueryWrapper<UserReadHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserReadHistory::getUserId, userId);
        queryWrapper.eq(UserReadHistory::getBookId, bookId);

        UserReadHistory readHistory = userReadHistoryMapper.selectOne(queryWrapper);

        if (readHistory != null){
            readHistory.setUpdateTime(LocalDateTime.now());
            readHistory.setPreContentId(preContentId);
            userReadHistoryMapper.updateById(readHistory);
        }else {
            readHistory = new UserReadHistory();
            readHistory.setUserId(userId);
            readHistory.setBookId(bookId);
            readHistory.setPreContentId(preContentId);
            readHistory.setCreateTime(LocalDateTime.now());
            readHistory.setUpdateTime(LocalDateTime.now());

            userReadHistoryMapper.insert(readHistory);
        }
    }
}
